Imports System.Data.SqlClient

Public Class TipoDocumento : Implements IBusquedaFiltrado

    Private IdTipoDoc As Integer
    Private Descrip As String
    Public Property TipoDoc() As Integer
        Get
            Return IdTipoDoc
        End Get
        Set(ByVal value As Integer)
            IdTipoDoc = value
        End Set
    End Property
    Public Property Descripcion() As String
        Get
            Return Descrip
        End Get
        Set(ByVal value As String)
            Descrip = value
        End Set
    End Property

    Public Sub New()
        IdTipoDoc = 0
        Descrip = ""
    End Sub
    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
                   Implements IBusquedaFiltrado.Buscar

        Dim comando As SqlCommand
        Dim rs As SqlDataReader
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM TipoDocumento WHERE IdTipoDoc ={0}", id)
        comando = New SqlCommand(sql, con.ConexionBase)
        rs = comando.ExecuteReader()
        comando.Dispose()


        If rs.Read Then
            IdTipoDoc = System.Convert.ToInt16(rs("IdTipoDoc"))
            Descrip = rs("Descrip").ToString
            lResp = True
        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function
    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "INSERT INTO TipoDocumento (IdTipoDoc,Descrip)" + _
              "  VALUES({0},'{1}')"
        sql = String.Format(sql, IdTipoDoc, Descrip)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "UPDATE TipoDocumento " + _
              "  SET Descrip = '{0}'," + _
              "  WHERE IdTipoDoc = {1}"
        sql = String.Format(sql, Descrip, IdTipoDoc)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
            Implements IBusquedaFiltrado.Eliminar

        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "DELETE FROM TipoDocumento WHERE IdTipoDoc ={0}"
        sql = String.Format(sql, id)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    Public Function BuscarDatos(ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = "SELECT * FROM TIPODOCUMENTO ORDER BY DESCRIP"
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "tipodocumento")

        Return datos
    End Function
    'Public Function BuscarId(ByVal DescTD As String, ByRef con As ConexionBD) As Boolean

    '    Dim comando As SqlCommand
    '    Dim rs As SqlDataReader
    '    Dim sql As String
    '    Dim lResp As Boolean = False

    '    sql = String.Format("SELECT * FROM TIPODOCUMENTO WHERE Descrip='{0}'", DescTD)
    '    comando = New SqlCommand(sql, con.ConexionBase)
    '    rs = comando.ExecuteReader()
    '    comando.Dispose()


    '    If rs.Read Then
    '        IdTipoDoc = System.Convert.ToInt16(rs("idtipodoc"))
    '        Descrip = rs("Descrip").ToString
    '        lResp = True
    '    Else
    '        lResp = False
    '    End If
    '    rs.Close()
    '    Return lResp

    'End Function
End Class
